#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   Set_up.py
@Time    :   2022/04/13 18:00:00
@Author  :   WangWei 
@Version :   1.0
@Contact :   wangwei@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''
# here put the import lib

import datetime
import  os
from ssl import CHANNEL_BINDING_TYPES
from xml.dom.expatbuilder import FilterVisibilityController
from commDriver.zlg.zlgcan import *


# HW config
#CAN
drive_type = ZCAN_USBCANFD_200U   # USE_TS_TC1014_DEVICE /   USE_TS_VIRTUAL_DEVICE / USE_TS_TC1011_DEVICE
drive_index = 0
chn_index = 0
can_abit = 500000
can_bbit = 2000000


power_Type = 'IT6720' # 2303 or IT6720
gpib_address = 'GPIB0::16::INSTR'


#Dbc define
root = os.getcwd()
project_DBC_File_CAN = os.path.join(root, r'data\Kunlun_ETC_BodyCANFD.dbc')
blfname = datetime.datetime.now().strftime('%y%m%d') + '.blf'
project_BLF_File_CAN = os.path.join(root, fr"blf\{blfname}")
# project_DBC_File_CAN = os.path.join(root, r'data\FX12_Matrix_CRRL_V1.6_20220531.dbc')
NetName = 'Vehicle_CAN'
nodeName= 'BCM'
# EnableNode = ['CGW_Safety','EPS_Safety','APA_Safety','ABS_ESP_Safety','FVC_Safety']
EnableNode = ['MRR','BCM']
EnableNode2 = ['EDR']

#Power Config
source='Power'           #电源名称，可以使用NIMAX查看
baudrate=9600

#Dbc UDS
reqID = 0x774
resID = 0x674
AFctID = 0x7FF

# 定义更多信号的配置: ID 数据 周期
signal_configs = [
                    (0x106, [0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00], 500, 'CANFD'),        # 车速信号:
                    (0x90,  [0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00], 10, 'CANFD'),         # FZCU_BodyCAN_0x90
                    (0x243, [0x00, 0x00, 0x67, 0x20, 0x00, 0x00, 0x03, 0x20], 50, 'CANFD'),         # FZCU_BodyCAN_0x243
                    (0x107, [0x00, 0x19, 0x03, 0x12, 0x09, 0x19, 0x20, 0x25], 100, 'CANFD'),        # XCD_BodyCAN_0x107
                    (0x1E6, [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], 1000, 'CANFD'),       # XCD_BodyCAN_0x107
                    (0x392, [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], 500, 'CANFD'),        # XCD_BodyCAN_0x392
                    (0x3A5, [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], 1000, 'CANFD'),       # XCD_BodyCAN_0x3A5
                    (0x3E6, [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08], 1000, 'CANFD'),       # XCD_BodyCAN_0x3E6
                    (0x560, [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], 1000, 'CANFD'),        # XCD_BodyCAN_0x392
                ]


jidianqiPort = 'COM4'
jidianqiBaudrate = 9600
jidianqiTimeOut = 0.3

CanPrint = True

# 0x22 DID
# 底层软件Bootload版本号 F180
BootSwId= [0x56,0x31,0x2E,0x32,0x30,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20] # 0xF180
# 备用零件号
CowinSparePartNumber = [0x46,0x30,0x32,0x2D,0x33,0x36,0x30,0x32,0x30,0x31,0x34,0x2D,0x30,0x31,0x41,0x41,0x20,0x20,0x20,0x20]  # 0xF187
# 软件版本号
ManufacturerECUSoftwareVersionNumber = [0x30,0x30,0x2E,0x30,0x30,0x2E,0x30,0x33] # 0xF188
# 生成日期
ProgrammingDate = [0x20,0x22,0x07,0x25]  # 0xF199
# 雷达角度
Radarangle = 0x2D # 0x3F1A

softname = 'MRR_HKHAU_HaiWai_App_1.00.01_Boot_1.1_CT_6.1.43.2_6.1.43.2_Pro.XCDII.zip'

zipfile_path = os.path.abspath(os.path.join(os.getcwd(),"./sw")) + '\\' +softname
error_path = os.path.abspath(os.path.join(os.getcwd(),"./sw")) + '\\' +'FailedSwPlus.zip'
oldzipfile_path = os.path.abspath(os.path.join(os.getcwd(),"./sw")) + '\\' +'old.zip'
errorApp_path = os.path.abspath(os.path.join(os.getcwd(),"./sw")) + '\\' +'FailedSwPlus\\001_Failed_App.hex'
errorBoot_path = os.path.abspath(os.path.join(os.getcwd(),"./sw")) + '\\' +'FailedSwPlus\\002_Failed_Boot.hex'

CTBoot=[6,1,43,2]

'''BYD产品DID'''
# 硬件版本号 F193 
ECUHardwareVersionNumber = '0c 18 03 05 00'# 美信【01 16 09 06 00】      70331【0b 17 09 0d 00】  4323【05 16 0c 1f 00 】 9840【06 16 0c 1f 00 】 '0b 17 0a 08 00':'MPC_70331_230Pro',
# 软件编码 F194
ECUSoftwareCode = '4A550013B870000000'
# DATASET软件序列号 FF94
DATASETSoftwareNumber = []
# DATASET软件版本号 FF95
DATASETSoftwareVersionNumber = []
# 底层软件Bootload版本号 F180
bootyear = [24]
bootmonth = [11]
bootday = [28]
bootchangeNum = [0]
bootver = [11]
bootweek = [48]
# bootweek.append(datetime.date(2000 + bootyear[0], bootmonth[0], bootday[0]).isocalendar().week)
VehicleManufacturerECUBootSoftwareVersionNumber = [0x62,0xF1,0X80] + bootyear + bootweek + bootchangeNum + bootver + [0] + [0] + bootyear + bootmonth + bootday

# 软件版本号 F195
APP_ver = '10001'
softver = []
hexver = hex(int(APP_ver)).replace('0x','').zfill(4)
softver.append(int(hexver[0:2],16))
softver.append(int(hexver[2:4],16))
softyear =[24]
softmonth = [11]
softday = [28]
ECUSoftwareVersionNumber = softver + softyear + softmonth + softday + [0]

# 生产模式 3401
FactoryMode = []
# 配置字
VariantCoding = [   ('13 B8 70 01 40 80 07 56'),               
                ]

# 初始存在故障
InitialFailure = [[0xD3,0x05,0x55,0x50],[0xD3,0x05,0x56,0x50],[0xD3,0x05,0x56,0x2F]]


Dut_Type = 'xiaomi'

DTC_list = [    ([0xD0,0xA2,0x87],'DLost Communication with FZCU on the Body CANFD bus'),			
                ([0xD0,0x97,0x87],'Lost Communication with XCD on the Body CANFD bus'),	
                ([0xD3,0x05,0x55],'VID Not Configured'),
                ([0xD3,0x05,0x56],'VID Invalid / Incompatible Configuration'),	
				([0xC0,0x7B,0x88],'CAN/CANFD I Bus Off'),	
                ([0xF0,0x03,0x17],'Battery voltage is high-Circuit Voltage Above Threshold '),		
				([0xF0,0x03,0x16],'Battery voltage is low-Circuit Voltage Below Threshold'),			
            ]

DTC_list2 = {   
                'DLost Communication with FZCU on the Body CANFD bus':      [0xD0,0xA2,0x87],
                'Lost Communication with XCD on the Body CANFD bus':        [0xD0,0x97,0x87],
                'CAN/CANFD I Bus Off':                                      [0xC0,0x7B,0x88],
                'Battery voltage is low-Circuit Voltage Below Threshold':   [0xF0,0x03,0x16],
                'Battery voltage is high-Circuit Voltage Above Threshold':  [0xF0,0x03,0x17],
                'VID Not Configured':                                       [0xD3,0x05,0x55],
                'VID Invalid / Incompatible Configuration':                 [0xD3,0x05,0x56],
}

PCAN_list = {   0x80: [0,True,0],    # rollcounter值  是否第一次=True:one, 出错次数
                               
}

PCAN_list_BOCH = {  0x64E: [0,True,0,8],
               
}

startCheck = False

MRR_0x66F_data = []
MRR_0x66F_time = []

MRR_0x5F0_data = []


